package com.sos.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sos.entity.po.Account;
import com.sos.entity.po.User;
import com.sos.mapper.UserMapper;
import com.sos.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 港某人
 * @date 2021/3/7 19:01
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, Object> implements UserService {


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String userRegister(Account account) {
        Account accountById = baseMapper.getAccountById(account.getAccount());
        if (accountById == null) {
            baseMapper.insertAccount(account);
            User user = new User(account);
            baseMapper.insertUser(user);
            return "注册成功";
        }
        return "已注册";
    }

    @Override
    public String login(Account account) {
        Account accountById = baseMapper.getAccountById(account.getAccount());
        if (accountById != null && accountById.getPassword().equals(account.getPassword())) {
            User userByAccount = baseMapper.getUserByAccount(account.getAccount());
            return userByAccount.getObjId();
        }
        return null;
    }

    @Override
    public User info(String objId) {
        return baseMapper.getUserById(objId);
    }

    @Override
    public void update(User user) {
        baseMapper.updateUser(user);
    }


}
